package com.captjack.shiro.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.security.Security;

/**
 * Description description.....
 *
 * @author Jack Sparrow
 * @version 1.0.0
 * @date 2018/6/7 22:50
 * package com.captjack.shiro.controller
 */
@RestController
@RequestMapping(value = "/user", method = {RequestMethod.GET})
public class LoginController {

    @RequestMapping(value = "/login")
    public Object login(String username, String password) {
        try {
            username = StringUtils.isEmpty(username) ? "admin" : username;
            password = StringUtils.isEmpty(password) ? "123456" : password;
            Subject user = SecurityUtils.getSubject();
            user.login(new UsernamePasswordToken(username, password));
        } catch (LockedAccountException lockedAccountException) {
            return "账号被锁定！";
        } catch (Exception exception) {
            return "登录异常，请稍后重试！";
        }
        return "登录成功！";
    }

}
